//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System;

namespace DNAide.Collections
{

	/// <summary>
	/// Dictionary helper methods
	/// </summary>
	public static partial class DictionaryAide
	{

		/// <summary>
		/// Serializes an object implementing IDictionary interface
		/// </summary>
		/// <param name="dictionary">dictionary</param>
		/// <returns>String</returns>
		public static String DictionarySerialize(IDictionary dictionary)
		{
			StringWriter sw = new StringWriter();
			DictionarySerialize(sw, dictionary);
			return sw.ToString();
		}

		/// <summary>
		/// Serializes an object implementing IDictionary interface
		/// </summary>
		/// <param name="writer">writer</param>
		/// <param name="dictionary">dictionary</param>
		public static void DictionarySerialize(TextWriter writer, IDictionary dictionary)
		{
			List<DictionaryAideEntry> entries = new List<DictionaryAideEntry>(dictionary.Count);
			foreach (object key in dictionary.Keys)
			{
				entries.Add(new DictionaryAideEntry(key, dictionary[key]));
			}
			XmlSerializer serializer = new XmlSerializer(typeof(List<DictionaryAideEntry>));
			serializer.Serialize(writer, entries);
		}

		/// <summary>
		/// De-serializes an object implementing IDictionary interface
		/// </summary>
		/// <param name="xmlString">xmlString</param>
		/// <param name="dictionary">dictionary</param>
		public static void DictionaryDeserialize(String xmlString, IDictionary dictionary)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(xmlString);
			System.IO.StringReader sReader = new System.IO.StringReader(xmlString);
			DictionaryDeserialize(sReader, dictionary);
		}

		/// <summary>
		/// De-serializes an object implementing IDictionary interface
		/// </summary>
		/// <param name="reader">reader</param>
		/// <param name="dictionary">dictionary</param>
		public static void DictionaryDeserialize(TextReader reader, IDictionary dictionary)
		{
			dictionary.Clear();
			XmlSerializer serializer = new XmlSerializer(typeof(List<DictionaryAideEntry>));
			List<DictionaryAideEntry> list = (List<DictionaryAideEntry>)serializer.Deserialize(reader);
			foreach (DictionaryAideEntry entry in list)
			{
				dictionary[entry.Key] = entry.Value;
			}
		}

	}

	/// <summary>
	/// Used in dictionary serialisation 
	/// </summary>
	public class DictionaryAideEntry
	{
		/// <summary>
		/// Key
		/// </summary>
		public object Key;
		/// <summary>
		/// Value
		/// </summary>
		public object Value;
		/// <summary>
		/// DictionaryAideEntry
		/// </summary>
		public DictionaryAideEntry() { }
		/// <summary>
		/// DictionaryAideEntry
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public DictionaryAideEntry(object key, object value)
		{
			Key = key;
			Value = value;
		}
	}
}
